package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.SalaryIndividualTax;
import com.sihai.ehr.model.biz.SalaryIndividualTaxRadix;
import com.sihai.ehr.model.biz.SalaryIndividualTaxRule;
import com.sihai.ehr.model.biz.SalaryOvertime;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.IndividualTaxService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletResponse;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Wang.Yi
 * Date: 2013-3-27
 * Time: 20:41:43
 * To change this template use File | Settings | File Templates.
 */
@Results({
        @Result(name = "main", location = "salary_main.jsp"),
        @Result(name = "list", location = "individual_tax_rule_manage_list.jsp"),
        @Result(name = "edit", location = "individual_tax_rule_edit.jsp")
})
public class SalaryIndividualTaxRuleAction extends EHRAction {

    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private IndividualTaxService individualTaxService;

    private List<Organization> organizations;
    private LimitedList<SalaryIndividualTaxRule> salaryIndividualTaxRules;

    private String title;//用来判断是新增还是修改

    private SalaryIndividualTaxRule salaryIndividualTaxRule;

    private String ruleIds;

    private SalaryIndividualTaxRadix salaryIndividualTaxRadix;

    @Action("/salary/individual_tax_rule_main")
    public String execute() throws Exception {
        listTitle = "个人所得税扣款规则管理";
        listAction = "/salary/individual_tax_rule_list.xhtml";
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = false;
        return "main";
    }

    @Action("/salary/individual_tax_rule_list")
    public String list() throws Exception {
        salaryIndividualTaxRadix = individualTaxService.getRadix(organizationId);//获取扣款基数
        salaryIndividualTaxRules = individualTaxService.queryTaxRule(organizationId, createLimit(), createSorter());

        return "list";
    }

    @Action("/salary/individual_tax_rule_edit")
    public String edit() throws Exception {
        if (getParameter("id") != null) {
            title = "修改";
            salaryIndividualTaxRule = individualTaxService.getRule(Long.valueOf(getParameter("id")));
        } else {
            title = "新增";
            salaryIndividualTaxRule = new SalaryIndividualTaxRule();
            Organization organization = new Organization();
            organization.setId(organizationId);
            salaryIndividualTaxRule.setOrganization(organization);
        }

        return "edit";
    }

    @Action("/salary/individual_tax_rule_save")
    public String save() throws Exception {
        if (salaryIndividualTaxRule == null) {
            return execute();
        }
        if (salaryIndividualTaxRule != null && salaryIndividualTaxRule.getId() != null) {
            SalaryIndividualTaxRule tmp = individualTaxService.getRule(salaryIndividualTaxRule.getId());
            BeanUtils.copyProperties(salaryIndividualTaxRule, tmp, new String[]{"id", "organization"});
            individualTaxService.saveRule(tmp);
        } else {
            individualTaxService.saveRule(salaryIndividualTaxRule);
        }
        return execute();
    }

    @Action("/salary/individual_tax_rule_remove")
    public String remove() throws Exception {
        if (StringUtils.isBlank(ruleIds)) {
            return execute();
        }
        individualTaxService.removeRule(ruleIds);
        return execute();
    }

    @Action(value = "/salary/individual_tax_radix_modify", results = {@Result(name = "success", type = "plainText"),@Result(name = "error", type = "plainText")})
    public String saveIndividualTaxRadix() throws Exception {
       
        Long orgId = 1L;
        Float radix = 0F;
        try {
            if (getParameter("orgId") != null) {
                orgId = Long.parseLong(getParameter("orgId"));
            }
            if (getParameter("radix") != null) {
                radix = Float.parseFloat(getParameter("radix"));
            }
        } catch (Exception e) {
            return ERROR;
        }
        System.out.println("orgid:"+orgId);
        System.out.println("radix:"+radix);
        try {
            SalaryIndividualTaxRadix temp = individualTaxService.getRadix(orgId);
            if (temp == null) {
                temp = new SalaryIndividualTaxRadix();
                Organization org = new Organization();
                org.setId(orgId);
                temp.setOrganization(org);

            }
            temp.setRadix(radix);
            individualTaxService.saveRadix(temp);

            HttpServletResponse response = ServletActionContext.getResponse();
            response.getWriter().write(
                    "{\"availability\":" + true + "}");
            response.getWriter().flush();
            response.getWriter().close();
            return SUCCESS;
        } catch (Exception e) {
            System.out.println(e);
            HttpServletResponse response = ServletActionContext.getResponse();
            response.getWriter().write(
                    "{\"availability\":" + false + "}");
            response.getWriter().flush();
            response.getWriter().close();
            return ERROR;
        }

    }


    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public LimitedList<SalaryIndividualTaxRule> getSalaryIndividualTaxRules() {
        return salaryIndividualTaxRules;
    }

    public void setSalaryIndividualTaxRules(LimitedList<SalaryIndividualTaxRule> salaryIndividualTaxRules) {
        this.salaryIndividualTaxRules = salaryIndividualTaxRules;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public SalaryIndividualTaxRule getSalaryIndividualTaxRule() {
        return salaryIndividualTaxRule;
    }

    public void setSalaryIndividualTaxRule(SalaryIndividualTaxRule salaryIndividualTaxRule) {
        this.salaryIndividualTaxRule = salaryIndividualTaxRule;
    }

    public String getRuleIds() {
        return ruleIds;
    }

    public void setRuleIds(String ruleIds) {
        this.ruleIds = ruleIds;
    }

    public SalaryIndividualTaxRadix getSalaryIndividualTaxRadix() {
        return salaryIndividualTaxRadix;
    }

    public void setSalaryIndividualTaxRadix(SalaryIndividualTaxRadix salaryIndividualTaxRadix) {
        this.salaryIndividualTaxRadix = salaryIndividualTaxRadix;
    }
}
